package org.example.myleet.p925;

class Solution {
    public boolean isLongPressedName(String name, String typed) {
        if (null == name) {
            return false;
        }
        if (name.length() > typed.length()) {
            //type cannot be shorter than name
            return false;
        }
        if (0 == name.length()) {
            //empty input always true
            return true;
        }
        if (name.charAt(0) != typed.charAt(0)) {
            //the first letter must be equal
            return false;
        }
        if (name.charAt(name.length() - 1) != typed.charAt(typed.length() - 1)) {
            //the last letter must be equal
            return false;
        }
        if (1 == name.length()) {
            //when only 1 letter
            return true;
        }
        //分别是name的上一个字符、name的当前字符、typed的当前字符
        char nlast, ncur, tcur;
        //分别是name的游标和typed的游标
        int ni = 1, ti = 1;
        while (ni < name.length() && ti < typed.length()) {
            nlast = name.charAt(ni - 1);
            ncur = name.charAt(ni);
            tcur = typed.charAt(ti);
            if (ncur == tcur) {
                //typed match
                //输入的位置和name的位置字符一致，通过此位置检查
                ++ni;
                ++ti;
            } else if (nlast == tcur) {
                //type repeatedly
                //输入位置和name的上一个字符一致，说明是重复输入，通过此位置检查
                ++ti;
            } else {
                //otherwise, no similarity
                //输入位置与name不能对应上，不属于重复输入情况
                return false;
            }
        }
        //完全检查通过
        return true;
    }
}
